/*
 * @Description: 日期工具类
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2023-07-17
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2025-04-25
 */
import dayjs from 'dayjs'

type DateType = string | number | Date | dayjs.Dayjs | undefined | null

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'
const DATE_FORMAT = 'YYYY-MM-DD'

export const formatToDateTime = (date: DateType = undefined, format = DATE_TIME_FORMAT) => dayjs(date).format(format)

export const formatToDate = (date: DateType = undefined, format = DATE_FORMAT) => dayjs(date).format(format)

export const formatDate = (_date: string) => {
  const date = new Date(_date)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0') // 月份是从0开始的
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

export const dateUtil = dayjs
